package com.hmsm.dingtalk;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dingtalk.open.client.api.service.corp.CorpConnectionService;
import com.dingtalk.open.client.common.ServiceException;
import com.hmsm.dingtalk.openapi.EnterpriseConfig;
import com.hmsm.server.util.URLHelp;

public class HmsmCorpConnectionService implements CorpConnectionService {
	
	private static final Log log = LogFactory.getLog(HmsmCorpConnectionService.class);

	@Override
	public String getCorpToken(String corpid, String corpsecret) throws ServiceException {
		String accessToken = null;
		
		URLHelp help = new URLHelp();

		String result;
		try {
			result = help.requestRul(EnterpriseConfig.OAPI_HOST+"/gettoken?corpid="+corpid+"&corpsecret="+corpsecret+"", "GET", "UTF-8");
		} catch (Exception e) {
			result = null;
			log.warn("访问失败："+e);
		}
		if (result!=null){
			JSONObject jsonResult = JSONObject.fromObject(result);
			if (jsonResult.containsKey("errcode")){
				if (jsonResult.getInt("errcode")==0){
					accessToken = jsonResult.getString("access_token");
				}else{
					log.warn("调用失败："+ jsonResult.getString("errmsg"));
				}
			}
		}
		
		return accessToken;
	}

}
